package com.hellobike.haxing.modules.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.purchase.entity.TPurchaseOrder;
import com.hellobike.haxing.modules.purchase.entity.TPurchasePro;
import com.hellobike.haxing.modules.purchase.mapper.TPurchaseOrderMapper;
import com.hellobike.haxing.modules.purchase.service.ITPurchaseOrderService;
import com.hellobike.haxing.modules.purchase.service.ITPurchaseProService;
import com.hellobike.haxing.modules.supplier.util.BeanUtils;
import com.hellobike.haxing.modules.supplier.util.Constant;
import com.hellobike.haxing.modules.supplier.util.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 采购订单表
 * @Author: haxing-boot
 * @Date:   2020-01-21
 * @Version: V1.0
 */
@Service
public class TPurchaseOrderServiceImpl extends ServiceImpl<TPurchaseOrderMapper, TPurchaseOrder> implements ITPurchaseOrderService {

    @Autowired
    private ITPurchaseProService tPurchaseProService;

    public Result<?> detail(String id){
        List<TPurchaseOrder> tPurchaseOrders = new ArrayList<>();
        TPurchaseOrder tPurchaseOrder = baseMapper.selectById(id);
        if(tPurchaseOrder==null) {
            return Result.error("未找到对应数据");
        }
        tPurchaseOrders.add(tPurchaseOrder);
        return Result.ok(tPurchaseOrders);
    }

    @Override
    public Result<?> genOrder(TPurchaseOrder tPurchaseOrder){
        QueryWrapper<TPurchasePro> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("supplier_id", tPurchaseOrder.getSupplierId());
        queryWrapper.eq("sku_id", tPurchaseOrder.getSkuId());
        TPurchasePro tPurchasePro = tPurchaseProService.getOne(queryWrapper);
        if(tPurchasePro != null) {
            TPurchaseOrder insertDo = new TPurchaseOrder();
            BeanUtils.copyProperties(tPurchaseOrder,insertDo);
            insertDo.setId(null);
            insertDo.setOrderCode(Convert.getOrderCodeByTime());
            insertDo.setBatchCode(Convert.getBatchCodeByTime());
            insertDo.setState(Constant.ORDER_STATE_CONFIRM);
            if(tPurchaseOrder != null && tPurchaseOrder.getIsDirectWarehousingStr()){
                insertDo.setIsDirectWarehousing(1);
            }else{
                insertDo.setIsDirectWarehousing(0);
            }
            baseMapper.insert(insertDo);

            if(tPurchasePro.getIsMissingParts().equals(Constant.IS_MISSING_PARTS_NO) || (tPurchaseOrder.getPurchaseVolume() != null && tPurchasePro.getMissingPartsVolume() < tPurchaseOrder.getPurchaseVolume())){
                return Result.error("采购数量大于缺货数量");
            }else{
                // 加上原商品列表里的 缺失数量
                TPurchasePro updateDo = new TPurchasePro();
                updateDo.setId(tPurchasePro.getId());
                int missingPartsVolume = tPurchasePro.getMissingPartsVolume()-tPurchaseOrder.getPurchaseVolume();
                if(missingPartsVolume == 0){
                    updateDo.setIsMissingParts(Constant.IS_MISSING_PARTS_NO);
                }
                if(tPurchasePro.getPurchaseOrderIds() != null) {
                    updateDo.setPurchaseOrderIds(tPurchasePro.getPurchaseOrderIds() + "," +insertDo.getOrderCode());
                }else{
                    updateDo.setPurchaseOrderIds(insertDo.getOrderCode());
                }
                updateDo.setMissingPartsVolume(missingPartsVolume);
                updateDo.setIsOrder(Constant.IS_ORDER_YES);
                tPurchaseProService.updateById(updateDo);
            }

        }
        return Result.ok("操作成功");
    }

}
